<?php

namespace App\Http\Controllers;

use DB;
use Illuminate\Http\Request;
use Validator;

class InformReplyController extends Controller
{
    public function index($informId)
    {
        $informResult = DB::table("im_inform")->select("send_count","replied_count")->where("id",$informId)->first();
        
        //通知回复项
        $informReplyOptResult = DB::table("im_reply_opt")
            ->select("id","content","is_expected","replied_count")
            ->where("inform_id",$informId)
            ->orderBy("is_expected","DESC")
            ->get();

        $messageReplyResult = [
            "0"=>["id"=>0,"content"=>"未回复","is_expected"=>0,"users"=>[]]
        ];

        foreach ($informReplyOptResult as $key => $value) {
            $value->users = [];
            $messageReplyResult[$value->id] = (array)$value;
        }

        //通知人回复情况
        $informUserResult = DB::table("im_inform_user")
            ->select("im_inform_user.user_id",
                "im_inform_user.reply_time","im_inform_user.attached_msg",
                "im_inform_user.reply_opt_id","im_inform_user.reply_method_id",
                "im_user.name AS user_name","im_user.icon AS user_icon",
                "im_method.name AS reply_method_name","im_method.icon AS reply_method_icon"
                )
            ->join("im_user","im_user.id","=","im_inform_user.user_id")
            ->leftJoin("im_method","im_method.id","=","im_inform_user.reply_method_id")
            ->where("im_inform_user.inform_id",$informId)
            ->orderBy("im_inform_user.reply_time","ASC")
            ->get();

        foreach ($informUserResult as $value) {
            $replyOptId = $value->reply_opt_id == null?0:$value->reply_opt_id;
            unset($value->reply_opt_id);
            $messageReplyResult[$replyOptId]["users"][] = $value;
        }
 
        $messageReplyReturn = [];
        foreach ($messageReplyResult as $value) {
            $messageReplyReturn[] = $value;
        }

        return $this::jsonResponse(false,["count"=>$informResult,"list"=>$messageReplyReturn]);
    }
}